package org.destiny.account.richclient.model;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class MyTableModel extends AbstractTableModel {
	// name;
	// gender;
	// township;
	// village;
	// remark;
	private static final long serialVersionUID = 1L;
	private boolean[] checked;
	private Vector<?> vector;
	private String[] fieldNames;
	private Method[] getters;
	private Method[] setters;

	public MyTableModel(Vector<?> v, Class<?> cls, String... fieldNames)
			throws NoSuchFieldException, SecurityException,
			IntrospectionException {
		checked = new boolean[v.size()];
		this.vector = v;
		this.fieldNames = fieldNames;
		getters = new Method[fieldNames.length];
		setters = new Method[fieldNames.length];
		for (int i = 0; i < fieldNames.length; i++) {
			Field f = cls.getField(fieldNames[i]);
			PropertyDescriptor pd = new PropertyDescriptor(f.getName(), cls);
			getters[i] = pd.getReadMethod();
			setters[i] = pd.getWriteMethod();
		}
	}

	@Override
	public int getRowCount() {
		return vector.size();
	}

	@Override
	public int getColumnCount() {
		return fieldNames.length;
	}

	@Override
	public Object getValueAt(int row, int col) {
		try {
			return getters[col].invoke(vector.get(row));
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		
	}
}